let oldY = 0;
let currentStatus;
let STATE_RECORDING = 1;
let STATE_WANT_TO_CANCEL = 2;
let isRecording = false;
let record = new Record("pangyou");
let audioImgMap = {
    1:"img/audio/yuyin_voice_1.png",
    2:"img/audio/yuyin_voice_2.png",
    3:"img/audio/yuyin_voice_3.png",
    4:"img/audio/yuyin_voice_4.png",
    5:"img/audio/yuyin_voice_5.png",
};

document.chanceImg = (level)=>{
    setTimeout(()=>{
        if(isRecording){
            if(currentStatus === STATE_RECORDING){
                if(level <= 0 || level >= 6){
                    level = 5;
                }
                document.dialog().$('audioImg').set({src:audioImgMap[level]});
            }
        }
        document.chanceImg(parseInt(Math.random() * 8));
    },400);
};

document.changeState = (state)=>{
    if(currentStatus !== state){
        currentStatus = state;
        switch(currentStatus){
            case STATE_RECORDING:
                document.dialog().$('audioText').get('cp').set({p:'手指上划,取消发送'});
                document.chanceImg(parseInt(Math.random() * 8));
                break;
            case STATE_WANT_TO_CANCEL:
                document.dialog().$('audioText').get('cp').set({p:'松开手指,取消发送'});
                document.dialog().$('audioImg').set({src:'img/audio/yuyin_cancel.png'});
                break;
        }
    }
};

document.$('chat').get('limg',1).touch((event)=>{
    console.log(event);
    switch(event.action){
        case 0:
            oldY = event.y;
            document.dialog('pangyou/chat-audio-dialog.xml');
            isRecording = true;
            record.prepare();
            break;
        case 2:
            if(oldY - event.y >= 300){
                document.changeState(STATE_WANT_TO_CANCEL);
            }else{
                document.changeState(STATE_RECORDING);
            }
            break;
        case 1:
            document.dialog().next();
            isRecording = false;
            console.log(record.getUrl());
            media.play(record.getUrl());
            break;
    }
});